﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using Weapp.Data;
using Weapp.Web.Models;

namespace Weapp.Web.Controllers
{
    public class SizeCategoryController : BaseApiController
    {
        public SizeCategoryController(IWeappRepository repo) : base(repo)
        {
        }

        public IEnumerable<SizeCategoryModel> Get()
        {
            var query = TheRepository.GetAllSizeCategories();
            var results = query
            .ToList()
            .Select(s => TheModelFactory.Create(s));
            return results;
        }

        public HttpResponseMessage GetSizeCategory(int id)
        {
            try
            {
                var sizeCategory = TheRepository.GetSizeCategory(id);
                if (sizeCategory != null)
                {
                    return Request.CreateResponse(HttpStatusCode.OK, TheModelFactory.Create(sizeCategory));
                }
                else
                {
                    return Request.CreateResponse(HttpStatusCode.NotFound);
                }

            }
            catch (Exception ex)
            {
                return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex);
            }
        }
        public HttpResponseMessage Post([FromBody] SizeCategoryModel sizeCategoryModel)
        {
            try
            {
                var entity = TheModelFactory.Parse(sizeCategoryModel);

                if (entity == null) Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Could not read size category from body");

                if (TheRepository.Insert(entity) && TheRepository.SaveAll())
                {
                    return Request.CreateResponse(HttpStatusCode.Created, TheModelFactory.Create(entity));
                }
                else
                {
                    return Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Could not save to the database.");
                }
            }
            catch (Exception ex)
            {

                return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex);
            }
        }
        [HttpPatch]
        [HttpPut]
        public HttpResponseMessage Put(int id, [FromBody] SizeCategoryModel sizeCategoryModel)
        {
            try
            {

                var sizeCategory = TheModelFactory.Parse(sizeCategoryModel);

                if (sizeCategory == null) Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Could not read subject/tutor from body");

                var originalSizeCategory = TheRepository.GetSizeCategory(id);

                if (originalSizeCategory == null || originalSizeCategory.SizeCategoryId != id)
                {
                    return Request.CreateResponse(HttpStatusCode.NotModified, "Course is not found");
                }
                else
                {
                    sizeCategory.SizeCategoryId = id;
                }

                if (TheRepository.Update(originalSizeCategory, sizeCategory) && TheRepository.SaveAll())
                {
                    return Request.CreateResponse(HttpStatusCode.OK, TheModelFactory.Create(sizeCategory));
                }
                else
                {
                    return Request.CreateResponse(HttpStatusCode.NotModified);
                }

            }
            catch (Exception ex)
            {
                return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex);
            }
        }
        public HttpResponseMessage Delete(int id)
        {
            try
            {
                var course = TheRepository.GetSizeCategory(id);

                if (course == null)
                {
                    return Request.CreateResponse(HttpStatusCode.NotFound);
                }

                if (TheRepository.Delete(id) && TheRepository.SaveAll())
                {
                    return Request.CreateResponse(HttpStatusCode.OK);
                }
                else
                {
                    return Request.CreateResponse(HttpStatusCode.BadRequest);
                }

            }
            catch (Exception ex)
            {
                return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex.Message);
            }
        }
    }
}
